package br.com.condominio.dao;

// default package
// Generated 19/04/2012 00:23:01 by Hibernate Tools 3.4.0.CR1

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import br.com.condominio.entity.Apartamento;

@Repository("apartamentoDao")
public class ApartamentoDAO {

	private static final Log log = LogFactory.getLog(ApartamentoDAO.class);

	@PersistenceContext
	private EntityManager entityManager;

	@SuppressWarnings("unchecked")
	@Transactional(readOnly = true)
	public List<Apartamento> findAll() {
		log.debug("findAll Apartamento instance");
		try {
			String jpql = "SELECT o FROM Apartamento o ORDER BY o.bloco.residencial.resNome, o.bloco.bloDescricao, o.aparNumero";
			Query query = entityManager.createQuery(jpql);
			List<Apartamento> apartamentos = (List<Apartamento>) query
					.getResultList();
			return apartamentos;
		} catch (RuntimeException re) {
			log.error("findAll failed", re);
			throw re;
		}
	}
	
	@SuppressWarnings("unchecked")
	@Transactional(readOnly = true)
	public List<Apartamento> findByNumero(Integer numero) {
		log.debug("findByNumero Apartamento instance");
		try {
			String jpql = " SELECT o FROM Apartamento o WHERE aparNumero = :aparNumero ORDER BY o.bloco.residencial.resNome, o.bloco.bloDescricao, o.aparNumero";
			Query query = entityManager.createQuery(jpql);
			query.setParameter("aparNumero", numero);
			List<Apartamento> apartamentos = (List<Apartamento>) query
					.getResultList();
			return apartamentos;
		} catch (RuntimeException re) {
			log.error("findAll failed", re);
			throw re;
		}
	}

	@Transactional
	public Apartamento persist(Apartamento transientInstance) {
		log.debug("persisting Apartamento instance");
		try {
			entityManager.persist(transientInstance);
			log.debug("persist successful");
			return transientInstance;
		} catch (RuntimeException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	
	@Transactional
	public void delete(Apartamento apartamento) {
		apartamento = entityManager.find(Apartamento.class,
				apartamento.getAparCodigo());
		entityManager.remove(apartamento);
	}

	@Transactional
	public Apartamento merge(Apartamento detachedInstance) {
		log.debug("merging Apartamento instance");
		try {
			Apartamento result = entityManager.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	@Transactional
	public Apartamento findById(Integer id) {
		log.debug("getting Apartamento instance with id: " + id);
		try {
			Apartamento instance = entityManager.find(Apartamento.class, id);
			log.debug("get successful");
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
}
